<?php


namespace app\common\cachemodel;


use app\common\cachemodel\Recode as RecodeCacheModel;
use app\common\lib\astrolog\Astrolog;
use app\common\model\BaseCache;
use think\Db;
use think\Exception;
use think\Log;

class ZlRecordShareAdd extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_record_share_add';

    protected $noNeedJudgeSummer = ['香港特别行政区', '澳门特别行政区', '台湾省', '北美洲', '大洋洲', '非洲', '南美洲', '欧洲', '亚洲', ''];

    protected $setting = [
        'natal' => [
            'planet' => [
                [1, 8.5], [1, 7.5], [1, 3.5], [1, 3.5], [1, 4], [1, 4.5], [1, 4.5], [1, 2.5], [1, 2.5], [1, 2.5],
                [0, 0], [0, 0], [0, 0], [0, 0], [1, 2.5], [0, 0], [0, 0], [1, 2.5], [1, 2.5], [1, 2.5], [1, 2.5]
            ],
            'phase' => [
                0 => [1, 0], 60 => [1, 0], 90 => [1, 0], 120 => [1, 0], 180 => [1, 0],
                45 => [0, 0], 135 => [0, 0], 30 => [0, 0], 150 => [0, 0], 72 => [0, 0], 144 => [0, 0]
            ],
            'arabic' => [
                [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0],
                [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]
            ],
            'h_sys' => 'p',
            'firdaria' => 'default',
            'bound_type' => 'default',
            'ecliptic' => 0,
            'profection_push_direction' => 0,
            'decanates' => 0,
            'ayanamsa' => 0,
            'location_use' => 0
        ],
    ];

    /**
     * 档案是否添加过了
     * @param int $type 1=>微信 2=>QQ
     * @param string $code 用户身份标识
     * @param int $time 分享时间|分享标识
     * @return bool true 已添加 false未添加过
     */
    public function hasAddRecord(int $type, string $code, int $time): bool
    {
        try
        {
            $data = $this->where('type', $type)->where('code', $code)->where('time', $time)->field('id')->find();
        }
        catch (Exception $e)
        {
            $data = null;
        }

        return $data ? true : false;
    }

    /**
     * 插入数据
     * @param $map
     * @return bool|int
     */
    public function SaveData($map)
    {
        try
        {
            $id = $this->insertGetId($map);
        }
        catch (\Exception $e)
        {
            return false;
        }
        return $id;
    }

    /**
     * 注册添加档案
     * @param string $avatar 头像
     * @param string $nickname 昵称
     * @param int $uid 用户ID
     * @param int $type 分享添加类型 1=>微信 2=>QQ
     * @param string $code 用户身份标识
     * @return bool   true 则说明添加档案成功 false 添加档案失败
     * @throws \Exception
     */
    public function registAddRecord(string $avatar, string $nickname, int $uid, int $type, string $code): bool
    {
        try
        {
            $info = $this->where('type', $type)->where('code', $code)->order('id desc')
                ->field('sex,year,month,day,hour,minute,birth_country,birth_province,birth_city,live_country,live_province,live_city,long_deg,long_min,ew,lat_deg,lat_min,ns')
                ->find();

            if ($info)
            {
                $is_summer = 0;
                // 只判断大陆地区的夏令时
                if (!in_array($info['birth_country'], $this->noNeedJudgeSummer, true))
                {
                    $is_summer = isSummerTime($info['hour'], $info['minute'], 0, $info['month'], $info['day'], $info['year']) ? 1 : 0;// 是否开启夏令时
                }

                $data = [
                    'status' => 1,
                    'uid' => $uid,
                    'isself' => 1,
                    'name' => $nickname,
                    'type' => $info['sex'],
                    'year' => $info['year'],
                    'month' => $info['month'],
                    'day' => $info['day'],
                    'hour' => $info['hour'],
                    'minute' => $info['minute'],
                    'timezone' => 8,
                    'birth_country' => $info['birth_country'],
                    'birth_province' => $info['birth_province'],
                    'birth_city' => $info['birth_city'],
                    'live_country' => $info['live_country'],
                    'live_province' => $info['live_province'],
                    'live_city' => $info['live_city'],
                    'comments' => '自己',
                    'long_deg' => $info['long_deg'],
                    'long_min' => $info['long_min'],
                    'ew' => $info['ew'],
                    'lat_deg' => $info['lat_deg'],
                    'lat_min' => $info['lat_min'],
                    'ns' => $info['ns'],
                    'is_summer' => $is_summer,
                    'avatar' => $avatar,
                    'from' => 'zhunle',
                    'add_type' => 3,
                    'is_read' => 1,
                ];

                // 查询当前星盘日月水金火木土等星落座落宫存入数据库
                $astro_data = Astrolog::getInstance()->setExecPrefix(config('astrolog.prefix'))
                    ->setIsAstrolabe(false)
                    ->setRecode1($data)
                    ->setUserSetting($this->setting)
                    ->getTheRecodeAstro();

                $RecodeCacheModel = new RecodeCacheModel();
                //插入档案信息
                $rid = $RecodeCacheModel->SaveData($uid, $data);
                if ($rid === false)
                {
                    return false;
                }

                if (!$rid)
                {
                    return false;
                }

                $astro_data['rid'] = $rid;
                $time = time();
                $astro_data['create_time'] = $time;
                //档案附属表
                Db::table('app_recode_info')->insertGetId(['rid' => $rid, 'create_time' => $time, 'last_time' => $time]);// 新增当前档案访问数据
                //插入档案星盘数据
                $aid = Db::table('app_recode_astro')->insertGetId($astro_data);// 新增当前档案行星数据
                $data['id'] = $rid;
                $RecodeCacheModel->UpdateData($uid, $rid, ['aid' => $aid]);

                return true;
            } else
            {
                return false;
            }
        }
        catch (Exception $e)
        {
            Log::write('注册自动添加档案失败：' . $e->getMessage(), 'error', true);
            return false;
        }
    }
}
